<template>
  <div class="seachHistory">
    <van-cell title="搜索历史">
      <div v-if="isDelete">
        <span class="deleteAll" @click="$emit('clear')">全部删除</span>
        <span @click="isDelete = false">完成</span>
      </div>
      <van-icon name="delete" v-else @click="isDelete = true" />
    </van-cell>
    <van-cell
      v-for="(historyStr, i) in searchHistories"
      :key="i"
      @click="deleteOrSearch(historyStr, i)"
    >
      <span slot="title">{{ historyStr }}</span>
      <van-icon name="close" v-if="isDelete" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isDelete: false
    }
  },
  created() {},
  mounted() {},
  activited() {},
  update() {},
  beforeRouteUpdate() {},
  methods: {
    deleteOrSearch(historyStr, i) {
      if (this.isDelete) {
        // 执行删除操作
        this.searchHistories.splice(i, 1)
        // 调用接口,实现数据持久化
      } else {
        // 执行搜索操作
        this.$emit('historySearch', historyStr)
      }
    }
  },
  filter: {},
  computed: {},
  watch: {}
}
</script>

<style lang="less" scoped>
.deleteAll {
  margin-right: 10px;
}
</style>
